package com.lmx.videoplay.video;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;

import com.lmx.videoplay.AppDataSourse;
import com.lmx.videoplay.R;
import com.lmx.videoplay.VideoBean;
import com.lmx.videoplay.library.SlidingRecyclerView;
import com.lmx.videoplay.util.AppConfig;
import com.lmx.videoplay.util.Constant;
import com.lmx.videoplay.util.XUtilsManager;

public class VideoDetailsActivity extends Activity implements VideoDetailsAdapter.OnPageSelectedLister, View.OnClickListener {

    private String TAG = getClass().getSimpleName();
    private SlidingRecyclerView mRvVideo;
    private VideoDetailsAdapter adapter;
    private SeekBar seekbarDesk;
    private Button btn1;
    private Button btn2;
    private VideoBean videoBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_details);
        initView();
    }

    private void initView() {
        findViewById(R.id.ibBack).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mRvVideo = findViewById(R.id.rvVideo);
        adapter = new VideoDetailsAdapter(this, this);
        mRvVideo.setAdapter(adapter);
        adapter.setData(AppDataSourse.getVideoList());
        seekbarDesk = (SeekBar) findViewById(R.id.seekbar_desk);
        btn1 = (Button) findViewById(R.id.btn_1);
        btn1.setOnClickListener(this);
        btn2 = (Button) findViewById(R.id.btn_2);
        btn2.setOnClickListener(this);

        seekbarDesk.setProgress(AppConfig.getVoice());
        seekbarDesk.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                Log.e(TAG, "当前进度:" + progress);
                AppConfig.setVoice(progress);
                if (progress == 0) {
                    WallpaperServiceTest.setVolume(VideoDetailsActivity.this, false);//无声模式
                } else {
                    WallpaperServiceTest.setVolume(VideoDetailsActivity.this, true);//有声模式
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                Log.e(TAG, "起始进度:" + seekBar.getProgress());
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                Log.e(TAG, "结束进度:" + seekBar.getProgress());
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        adapter.release();
    }

    @Override
    public void onPageSelected(int position) {
        videoBean = adapter.videoBeanList.get(position);
        Log.e(getClass().getSimpleName(), "当前显示的视频数据:" + position + "," + adapter.videoBeanList.get(position).getVideoUrl());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_1:
                final String fileName = Constant.getDCIM() + "/" + System.currentTimeMillis() + ".mp4";
                Log.e(TAG, "开始下载:" + fileName);
                /*WallpaperServiceTest.startWallPaper(VideoDetailsActivity.this,fileName);*/

                XUtilsManager.getInstance().getDownLoad(200, new XUtilsManager.HttpCallBack() {
                    @Override
                    public void onEndDownload(int what, String end) {
                        Log.e(TAG, "下载结束:" + end);
                    }

                    @Override
                    public void onStartDownload(int what, String start) {
                        Log.e(TAG, "下载开始:" + start);
                    }

                    @Override
                    public void onSchedule(int what, int schedule) {
                        Log.e(TAG, "正在下载:" + schedule + "%");
                    }

                    @Override
                    public void onResponse(int what, String response) {
                        Log.e(TAG, "下载完成:" + response);
                        WallpaperServiceTest.startWallPaper(VideoDetailsActivity.this, response);
                    }

                    @Override
                    public void onFailure(int what, String error) {
                        Log.e(TAG, "下载失败:" + error);
                    }
                }, fileName, videoBean.getVideoUrl());
                break;
            case R.id.btn_2:
                //设置锁屏壁纸
                final String fileName1 = Constant.getDCIM() + "/" + "a2.mp4";
                Log.e(TAG, "开始下载:" + fileName1);
                WallpaperServiceTest.startWallPaper(VideoDetailsActivity.this, fileName1);
                break;
        }
    }
}